//
//  CWSportingVC.m
//  大神专制黑马行
//
//  Created by chenWei on 2017/5/10.
//  Copyright © 2017年 陈伟. All rights reserved.
//

#import "CWSportingVC.h"
#import "CWSportMapVC.h"
#import "UIColor+Addition.h"
#import "CWSportSpeaker.h"

@interface CWSportingVC ()<CWSportMapVCDelegate>

@property(nonatomic, strong) CWSportMapVC *mapVC;

//容器视图
@property (weak, nonatomic) IBOutlet UIView *containView;
//距离
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
//时长
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
//平均速
@property (weak, nonatomic) IBOutlet UILabel *avgSpeedLabel;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finishCenterXContraint;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *continueCenterXContraint;

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *sportStateButtonArr;

//是否暂停
@property(nonatomic, assign) BOOL isPause;


//运动语音播报器
@property(nonatomic, strong) CWSportSpeaker *sportSpeaker;

@end

@implementation CWSportingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加运动地图控制器
    [self addSportMapVC];
    
    //设置背景梯度颜色图层
    [self configColor];
    
    //运动开始
    self.sportSpeaker = [[CWSportSpeaker alloc]initWithDistanceUnit:1 SportType:self.sportType];
    [self.sportSpeaker sportStart];
    
}

#pragma mark - 跳转到地图控制器
- (IBAction)sportMapVC:(UIButton *)sender {
    //点击跳转到地图控制器

    //3.跳转
    [self presentViewController:self.mapVC animated:YES completion:nil];
    
    //4.设置罗盘的显示位置
    self.mapVC.mapView.compassOrigin = CGPointMake(sender.center.x - self.mapVC.mapView.compassSize.width*0.5, sender.center.y - self.mapVC.mapView.compassSize.height*0.5);
    
}

#pragma mark - 运动状态监听
- (IBAction)sportStateButtonClick:(UIButton *)sender {
    
    self.mapVC.sportTrackingModel.sportState = sender.tag;
    
    if (sender.tag == SportStatePause) {//暂停
        [self animationWithFold:NO];
    }else if(sender.tag == SportStateContinue) //继续
    {
        [self animationWithFold:YES];
    }else{ //结束
        [self dismissViewControllerAnimated:YES completion:nil];
    }
 
    
    //改变运动状态
    [self.sportSpeaker sportStateChange:sender.tag];
    
}


#pragma mark - 按钮运动状态动画
/**
 按钮动画
 
 @param isFold 是否折叠 YES：折叠，NO：不折叠
 */
- (void)animationWithFold:(BOOL)isFold
{
    //暂停按钮
    UIButton *pauseBtn = _sportStateButtonArr[0];
    
    if (isFold ==  NO) {
        //点击的是暂停
        self.finishCenterXContraint.constant += 100;
        self.continueCenterXContraint.constant -= 100;
        //隐藏暂停按钮
        pauseBtn.hidden = YES;

    }else {
        //点击的是继续
        self.finishCenterXContraint.constant -= 100;
        self.continueCenterXContraint.constant += 100;
        
    }
    
    //在动画未完成之前取消用户交互
    self.view.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:0.3 animations:^{
        //让视图计算提前
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        if (isFold == YES) {
            //显示暂停按钮
            pauseBtn.hidden = NO;
        
        }
        
        self.view.userInteractionEnabled = YES;
    }];
    
}


#pragma mark - 实现地图控制器的代理方法
- (void)sportMapVcWith:(CWSportMapVC *)sportMapVC SportTrackingModel:(CWSportTrackingModel *)sportTrackingModel
{
    //赋值
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f",sportTrackingModel.totalDistance];
    self.timeLabel.text = sportTrackingModel.totalTimeStr;
    self.avgSpeedLabel.text = [NSString stringWithFormat:@"%.2f",sportTrackingModel.avgSpeed];
    
    
    //播报整点距离
    [self.sportSpeaker sportWithDistance:sportTrackingModel.totalDistance TotalTime:sportTrackingModel.totalTime AvgSpeed:sportTrackingModel.avgSpeed];
    
}





//添加运动地图控制器
- (void)addSportMapVC
{
#pragma mark - 解决点击跳转按钮跳转，返回再跳转的时候会重新创建地图控制器的 bug
    //1.实例化控制器
    self.mapVC = [[UIStoryboard storyboardWithName:@"CWSport" bundle:nil] instantiateViewControllerWithIdentifier:@"CWSportMapVC"];
    //设置地图控制器的代理
    self.mapVC.delegate = self;
    
    //2.创建模型
    self.mapVC.sportTrackingModel = [[CWSportTrackingModel alloc]initWithSportType: self.sportType SportState:SportStateContinue];
    
    //MARK: - 加载 storyboard
    
    //把 sportMapVC 设置为当前控制器的子控制器
 /*
    //1.先加载
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"CWSport" bundle:nil];
    CWSportMapVC *sportMapVC = [storyBoard instantiateViewControllerWithIdentifier:@"CWSportMapVC"];
    
    self.mapVC = sportMapVC;
    
    sportMapVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 60);
    
    //2.把控制器添加到当前控制器中
    [self addChildViewController:sportMapVC];
    //3.把CWSportMapVC的视图添加当前控制器视图中
    [self.view addSubview:sportMapVC.view];
    //4.把CWSportMapVC 添加到父控制器中
    [sportMapVC didMoveToParentViewController:self];
  
    sportMapVC.sportTrackingModel = [[CWSportTrackingModel alloc]initWithSportType: self.sportType SportState:SportStateContinue];
    
  */
    
    //MARK: - 加载容器视图
    //方式1
//    self.mapVC = self.childViewControllers[0];
//    //方式2
//    //通过容器视图拿到地图控制器
//    self.mapVC = (CWSportMapVC *)self.containView.subviews[0].nextResponder;
//    
//    self.mapVC.sportTrackingModel = [[CWSportTrackingModel alloc]initWithSportType: self.sportType SportState:SportStateContinue];
//    
}

#pragma mark - 设置图层梯度渐变颜色
- (void)configColor
{
    //1.创建梯度图层
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc]init];
    
    //2.设置图层的大小和位置
    gradientLayer.bounds = self.view.bounds;
    gradientLayer.position = self.view.center;
    
    //3.创建三个梯度颜色
    CGColorRef color1 = [UIColor colorWithHex:0x0e1428].CGColor;
    CGColorRef color2 = [UIColor colorWithHex:0x406479].CGColor;
    CGColorRef color3 = [UIColor colorWithHex:0x406578].CGColor;

    //4.添加到颜色数组
    gradientLayer.colors = @[(__bridge UIColor*)color1, (__bridge UIColor*)color2, (__bridge UIColor*)color3];
    
    //5.设置位置数组
    gradientLayer.locations = @[@0, @0.6, @1];
    
    //5.把梯度图层插入到当前视图图层
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
    
}

#pragma mark - 运动状态 /开始或暂停，继续运动
//- (IBAction)startOrPauseOrContinueSport:(UIButton *)sender {
//    //赋值
//    self.mapVC.sportTrackingModel.sportState = sender.tag;
//    
//    if (sender.tag == SportStateStop) {
//        [self dismissViewControllerAnimated:YES completion:nil];
//    }
//    
//}


#pragma mark - segue unwind用法
//用法：1.只需要方法类型是 IBAction
//     2.方法的参数必须是 segue 本身
- (IBAction)dissmiss:(UIStoryboardSegue *)segue
{
    //无需任何代码
}




@end
